/**
 * виджет облака тегов
 */

;(function($){

  $.extend(String.prototype, {replaceAll: function(s,r){return this.replace(new RegExp(s,"g"),r);}});

  var swf      = $.sub();
  swf.fn.embed = function(o)
  {
    o        = $.extend({version: "9.0.0"}, o);
    o.params = $.extend({
      quality          : "high",
      wmode            : "transparent",
      scale            : "show-all",
      menu             : false,
      play             : true,
      loop             : true,
      allowScriptAccess: "sameDomain"
    }, o.params);
    return this.each(function()
    {
      var $this = $(this);
      if (!$this.html())
        $this.html("<a href='http://www.adobe.com/go/getflashplayer'><img src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>");
      var classAttr = $this.attr("class");
      var styleAttr = $this.attr("style");
      swfobject.embedSWF(
        o.src,
        $this.attr("id"),
        o.width  || $this.width () || 1,
        o.height || $this.height() || 1,
        o.version,
        o.install,
        o.flashVars,
        o.params,
        o.attrs,
        function(obj)
        {
          if (obj.success)
          {
            var $obj = $(["#", obj.id].join("")).attr({"class": classAttr, "style": styleAttr});
            if ($.isFunction(o.callback))
              o.callback.call($obj[0]);
          }
        }
      );
    });
  }
  $.extend($.fn, {swf: function(){return swf(this);}});

  $.widget("swf.tagcloud", {
    options: {swfpath: "", swffile: "tagcloud.swf", color1: "#000000", color2: "#000000", hcolor: "#000000", speed: 100},
    _create: function()
             {
               var tags = encodeURIComponent(["<tags>", "</tags>"].join(this.element.html().replaceAll("<A(.+?)>(.+?)</A>", "<a$1>$2</a>").replaceAll("\s+(\w+=)(.+?)\s+", " $1=\"$2\" ")));
               $.each({"21": "!", "27": "'", "28": "\\(", "29": "\\)", "2A": "\\*"}, function(code,ch){tags=tags.replaceAll(ch,["%",code].join(""));});
               var r = Math.floor(Math.random() * 9999999);
               this.element.swf().embed({
                 src      : [this.options.swfpath, this.options.swffile, "?r=", r].join(""),
                 install  : this.options.install,
                 flashVars: {
                   tcolor  : ["0x", this.options.color1.substr(1, 6)].join(""),
                   tcolor2 : ["0x", this.options.color2.substr(1, 6)].join(""),
                   hicolor : ["0x", this.options.hcolor.substr(1, 6)].join(""),
                   tspeed  : this.options.speed,
                   distr   : true,
                   mode    : "tags",
                   tagcloud: tags
                 },
                 params   : this.options.bgcolor ? {bgcolor: this.options.bgcolor} : null,
                 attrs    : {id: [this.element.attr("id"), "_cloud", r].join("")}
               });
             }
  });

})(jQuery);
